/**
 * @Author: dn-jinmin
 * @File:  pkg
 * @Version: 1.0.0
 * @Date: 2024/4/15
 * @Description:
 */

package pkg

import (
	"bytes"
	"encoding/json"
	"fmt"
	"log"
)

const (
	DevMode = "dev"
	ProMode = "pro"
)

var Mode = DevMode

func JsonPrint(data ...interface{}) string {
	var res string
	for i, datum := range data {
		if i == 0 {
			res += "【"
		}

		switch datum.(type) {
		case string, int, int64, int32, uint, uint32, uint64:
			fmt.Println(datum)
			res += fmt.Sprintf("%v", datum)
		case error:
			fmt.Println("err : ", datum)
			res += fmt.Sprintf("err : %v", datum)
		default:
			b, err := json.Marshal(datum)
			if err != nil {
				res += fmt.Sprintf(" [ %v  json.Marshal err %v ]", datum, err)
				fmt.Println(err)
				continue
			}
			res += fmt.Sprintf("%v", string(b))
			var out bytes.Buffer
			err = json.Indent(&out, b, "", "   ")
			if err != nil {
				continue
			}
			fmt.Println(out.String())
		}

		if len(data) == i {
			res += "】"
		} else {
			res += " : "
		}
	}
	return res
}

func DD(desc interface{}, a ...interface{}) {
	if Mode != DevMode {
		return
	}

	if len(a) == 0 {
		fmt.Println("================== >>>>>>>>>> dd >>>>>>>>>>>>> =======")
		res := JsonPrint(desc)
		fmt.Println("================== <<<<<<<<<< dd <<<<<<<<<<<<< =======")

		log.Printf("dd %v", res)
		return
	}

	fmt.Println("================== >>>>>>>>>> ", desc, " >>>>>>>>>>>>> =======")
	res := JsonPrint(a...)
	fmt.Println("================== <<<<<<<<<< ", desc, " <<<<<<<<<<<<< =======")
	log.Printf(fmt.Sprintf("%v", desc)+"%v", res)
}

func DDF(handler func(), a ...interface{}) {
	if Mode != DevMode {
		return
	}

	if len(a) == 0 {
		handler()
		return
	}

	if len(a) == 1 {
		fmt.Println(a[0])
		log.Print(a[0])
		handler()
		fmt.Println(a[0])
		log.Print(a[0])
		return
	}

	fmt.Println(a[0])
	log.Print(a[0])
	handler()
	fmt.Println(a[1])
	log.Print(a[1])
}

func DC(desc interface{}, a ...interface{}) {

	if len(a) == 0 {
		fmt.Println("================== >>>>>>>>>> dd >>>>>>>>>>>>> =======")
		JsonPrint(desc)
		fmt.Println("================== <<<<<<<<<< dd <<<<<<<<<<<<< =======")
		return
	}

	fmt.Println("================== >>>>>>>>>> ", desc, " >>>>>>>>>>>>> =======")
	JsonPrint(a...)
	fmt.Println("================== <<<<<<<<<< ", desc, " <<<<<<<<<<<<< =======")
}

func CDD(flag bool, desc interface{}, a ...interface{}) {
	if flag {
		DD(desc, a...)
	}
}
func CDC(flag bool, desc interface{}, a ...interface{}) {
	if flag {
		DC(desc, a...)
	}
}
